//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================

#include <ddk.h>

class CMStatServer : public CDeviceDriver
{
public:
    CARAPI Read(
        /* [in] */ Int64 u64Offset,
        /* [in] */ Int32 bytesToRead,
        /* [out] */ MemoryBuf * pBuffer,
        /* [out] */ IEvent * * ppCompletionEvent);

    CARAPI Write(
        /* [in] */ Int64 u64Offset,
        /* [in] */ const MemoryBuf & buffer,
        /* [out] */ Int32 * pBytesWritten,
        /* [out] */ IEvent * * ppCompletionEvent);

    CARAPI Control(
        /* [in] */ Handle32 nControlCode,
        /* [in] */ const MemoryBuf & inBuffer,
        /* [out] */ MemoryBuf * pOutBuffer,
        /* [out] */ IEvent * * ppCompletionEvent);

    virtual void Dispose();
};

EXTERN IDeviceDriver * CDECL CreateMStatServer(uint_t uDeviceNo, void *pvParameter)
{
    CMStatServer *pStatServer = new CMStatServer;
    if (NULL == pStatServer) {
        kprintf("ERROR: Not enough memory!\n");
        return NULL;
    }

    pStatServer->AddRef();

    return pStatServer;
}

ECode CMStatServer::Read(
    /* [in] */ Int64 u64Offset,
    /* [in] */ Int32 bytesToRead,
    /* [out] */ MemoryBuf * pBuffer,
    /* [out] */ IEvent * * ppCompletionEvent)
{
    return E_NOT_IMPLEMENTED;
}

ECode CMStatServer::Write(
    /* [in] */ Int64 u64Offset,
    /* [in] */ const MemoryBuf & buffer,
    /* [out] */ Int32 * pBytesWritten,
    /* [out] */ IEvent * * ppCompletionEvent)
{
    return E_NOT_IMPLEMENTED;
}

ECode CMStatServer::Control(
    /* [in] */ Handle32 nControlCode,
    /* [in] */ const MemoryBuf & inBuffer,
    /* [out] */ MemoryBuf * pOutBuffer,
    /* [out] */ IEvent * * ppCompletionEvent)
{
    if (pOutBuffer == NULL || !pOutBuffer->GetCapacity()) {
        return E_INVALID_ARGUMENT;
    }

    if (nControlCode == 0) {
#ifdef KCONFIG_MSTAT
        StatInfo statInfo;
        GetStatInfo(&statInfo);
        pOutBuffer->Copy((Byte*)&statInfo, sizeof(statInfo));
#endif //KCONFIG_MSTAT
    }

    return NOERROR;
}

void CMStatServer::Dispose()
{
    delete this;
}
